package com.cskaoyan.mall.service.lmpl;

import com.cskaoyan.mall.dao.GoodsMapper;
import com.cskaoyan.mall.model.bo.goods.AskGoodsMsgBo;
import com.cskaoyan.mall.model.bo.goods.GoodsDetailsBo;
import com.cskaoyan.mall.model.bo.goods.GoodsTypeBo;
import com.cskaoyan.mall.model.bo.goods.UpdateGoodsDetailsBo;
import com.cskaoyan.mall.model.po.*;
import com.cskaoyan.mall.model.vo.goods.*;
import com.cskaoyan.mall.model.vo.order.OrderMessageVo;
import com.cskaoyan.mall.model.vo.user.UserMessageVo;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: GoodsServiceImpl
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/6/30 10:29
 * @Version
 **/
public class GoodsServiceImpl implements GoodsService {


    @Override
    public List<GoodsTypeVo> selectType() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsTypeVo> goods = mapper.selectTypeById(null);
        sqlSession.commit();
        sqlSession.close();
        return goods;
    }

    /**
    * @Author: 庄左
    * @Description: 通过类别的id查找该类的所有商品
    * @DateTime: 2022/7/2 21:46
    */
    @Override
    public List<GoodsMessageVo> selectGoodsByType(Type type) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsMessageVo> goods = mapper.selectGoodsByType(new Type(type.getId(), null));
        sqlSession.commit();
        sqlSession.close();
        return goods;
    }

    @Override
    public boolean addType(GoodsTypeBo goodsTypeBo) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        boolean flag = true;
        try {
            mapper.addType(new Type(null, goodsTypeBo.getName()));
        } catch (Exception e) {
            //插入失败
            flag = false;
            e.getMessage();
        }
        sqlSession.commit();
        sqlSession.close();
        return flag;
    }

    @Override
    public void addGoods(GoodsDetailsBo goods) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<GoodsDetailsBo.SpecListEntity> specList = goods.getSpecList();
        String price = null;
        String stockNum = null;
        for (GoodsDetailsBo.SpecListEntity specListEntity : specList) {
            stockNum = specListEntity.getStockNum();
            price = specListEntity.getUnitPrice();
        }
        mapper.addGoods(new Goods(null, Integer.valueOf(goods.getTypeId()), goods.getName(), goods.getImg(), goods.getDesc(), price, stockNum));
        sqlSession.commit();
        sqlSession.close();


    }


    @Override
    public void insertSpec(GoodsDetailsBo goods, Integer goodid) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsDetailsBo.SpecListEntity> specList = goods.getSpecList();
        List<Spec> specs = new ArrayList<>();

        for (GoodsDetailsBo.SpecListEntity specListEntity : specList) {
            String stockNum = specListEntity.getStockNum();
            String price = specListEntity.getUnitPrice();
            String specName = specListEntity.getSpecName();
            specs.add(new Spec(null, goodid, specName, price, stockNum));
        }
        mapper.insertSpec(specs);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
    * @Author: 庄左
    * @Description: 通过id 查找商品,用于在商品编辑时展示
    * @DateTime: 2022/7/2 21:41
    */
    @Override
    public GoodVo selectGood(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        GoodVo goodVo = mapper.selectGood(id);
        sqlSession.commit();
        sqlSession.close();
        return goodVo;
    }
/**
* @Author: 庄左
* @Description: 通过商品id查找该商品的规格列表,用于在编辑商品时展示
* @DateTime: 2022/7/2 21:42
*/
    @Override
    public List<SpecVo> selectSpec(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<SpecVo> specVos = mapper.selectSpec(id);
        sqlSession.commit();
        sqlSession.close();
        return specVos;
    }

    /**
    * @Author: 庄左
    * @Description: 删除商品，一并删除规格
    * @DateTime: 2022/7/3 15:01
    */
    @Override
    public void deteleGoods(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        mapper.deleteSpec(new Spec(id));
        mapper.deleteGoods(id);
        sqlSession.commit();
        sqlSession.close();

    }


    /**
    * @Author: 庄左
    * @Description: 单独删除规格
    * @DateTime: 2022/7/3 15:02
    */
    @Override
    public void deleteSpec(Integer goodsId, String specName) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        mapper.deleteSpec(new Spec(goodsId,specName));
        sqlSession.commit();
        sqlSession.close();
    }

//============================================================================================================================================//
    /**
    * @Author: 庄左
    * @Description: 编辑之后,根据传入的相关信息来更新商品信息
    * @DateTime: 2022/7/2 22:09
    */
    @Override
    public void selectUpdateGood(UpdateGoodsDetailsBo goods) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        mapper.selectUpdateGood(new Goods(goods.getId(), Integer.valueOf(goods.getTypeId()), goods.getName(), goods.getImg(), goods.getDesc(), null, null));
        sqlSession.commit();
        sqlSession.close();

    }
    @Override
    public void selectUpdateGoodSpec(UpdateGoodsDetailsBo goods, Integer goodid) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<UpdateGoodsDetailsBo.SpecListEntity> specList = goods.getSpecList();
        for (UpdateGoodsDetailsBo.SpecListEntity specListEntity : specList) {
            String stockNum = specListEntity.getStockNum();
            String price = specListEntity.getUnitPrice();
            String specName = specListEntity.getSpecName();
            mapper.selectUpdateGoodSpec(new Spec(null, goodid, specName, price, stockNum));
        }
        sqlSession.commit();
        sqlSession.close();
    }



    //===============================================================================================================================================//
    @Override
    public void deleteType(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        mapper.deleteType(id);
        sqlSession.commit();
        sqlSession.close();
    }

    //=========================================以上为后台系统的查询==============================//

    @Override
    public GoodVo selectGoodByGoodsIdInMall(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        GoodVo goodVo = mapper.selectGoodByGoodsIdInmall(id);
        sqlSession.commit();
        sqlSession.close();
        return goodVo;
    }

    @Override
    public List<CommentVo> selectGoodsCommentByGoodsId(Integer goodsId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<CommentVo> comment = mapper.selectGoodsCommentByGoodsId(goodsId);
        sqlSession.commit();
        sqlSession.close();
        return comment;
    }


    //商品提问
    @Override
    public int insertGoodsMsg(AskGoodsMsgBo askGoodsMsgBo) {
        GoodsMsg goodsMsg = new GoodsMsg();
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        //根据商品id查找商品的名字
        String goodsName = selectGoodsNameByGoodsId(askGoodsMsgBo.getGoodsId());
        //根据用户昵称查找id
        Integer userId = selectUserIdByUserNickName(askGoodsMsgBo.getToken());

        goodsMsg.setUserId(userId);
        goodsMsg.setGoodsId(askGoodsMsgBo.getGoodsId());
        goodsMsg.setContent(askGoodsMsgBo.getMsg());
        goodsMsg.setStateId(0);
        goodsMsg.setGoodsname(goodsName);
        goodsMsg.setUsername(askGoodsMsgBo.getToken());
        goodsMsg.setCreatetime(createTime);

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        int i = mapper.insertGoodsMsg(goodsMsg);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }



    /**
    * @Author: 庄左
    * @Description: 查找商品
    * @DateTime: 2022/7/9 16:19
    */
    @Override
    public List<GoodsSearchVo> selectGoodsByGoodsName(String keyword) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<GoodsSearchVo> goodsSearchVos = mapper.selectGoodsByGoodsName(keyword);
        sqlSession.commit();
        sqlSession.close();

        return goodsSearchVos;


    }


    private Integer selectUserIdByUserNickName(String token) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        Integer userId = mapper.selectUserIdByUserNickName(token);
        sqlSession.commit();
        sqlSession.close();

        return userId;

    }

    private String selectGoodsNameByGoodsId(Integer goodsId) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        String goodsName = mapper.selectGoodsNameByGoodsId(goodsId);
        sqlSession.commit();
        sqlSession.close();

        return goodsName;

    }

    //


    // @Override
    // public UserMessageVo selectUserNickNameByUserId(Integer userId) {
    //     SqlSession sqlSession = MyBatisUtils.getSqlSession();
    //     GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
    //     UserMessageVo name = mapper.selectUserNickNameByUserId(userId);
    //     sqlSession.commit();
    //     sqlSession.close();
    //     return name;
    // }


}
